<?php
/**
 * Move To Popup
 * 
 * This popup will display the list of available directories 
 * and label to move the message to it
 * 
 */

use Orx\Mailer\MailBox\Message\Background\Job;
use Fox\Html\Button;
use Orx\Mailer\MailBox\Message;
use Fox\Html\Input;
use Hive\Mission;
use Fox\Generator;
use Fox\Html\DropList;
use Fox\Html\Screen;
use Fox\Popup;
$xview = Xview::instance();
$showAll = $xview->val('showall');
$directories = array();
$screen = new Screen('sMoveTo','moveto',null,true,Generator::ajaxReload('contentCol'));
$screen->buttons('moveto')->label(Msg(Orx\Mailer\Messages::BUNDLE,Orx\Mailer\Messages::MOVE));
$screen->add(new Input('messageids'))
		->hidden();

$query = \Fox\Dao::infos(Mission::className())->handle()->createQueryBuilder()
	->select('missionid','reference','title')
	->from('missions','m')
	->orderBy('reference');
	
if($showAll) {
	$fragments[] = new \Fox\Dao\SqlFragment('loekz = ?',array(0));
} else {
	$fragments[] = new Fox\Dao\SqlFragment('status = ? AND loekz = ?',array(Mission::STATUS_OPEN,0));
}
$fragment = Fox\Dao\SqlFragment::combine($fragments);
$missions = $fragment->pushIn($query)->execute()->fetchAll(PDO::FETCH_ASSOC);
foreach($missions as $missionData) {
	$label = empty($missionData['reference']) ? $missionData['title'] : $missionData['reference'];
	$directories[] = array('id' => Mission::MESSAGE_REFERENCE.':'.$missionData['missionid'],'label' => $label);
}
$screen->add(new DropList('directories'))
		->label(Msg(Hive::BUNDLE,Hive::TARGETS_LOCATION))
		->multiple(true)
		->options($directories,'id','label');
$screen->add(new Button('moveandarchive'))
		->label(Msg(Orx\Mailer\Messages::BUNDLE,Orx\Mailer\Messages::MOVE_AND_ARCHIVE));
$screen->buttonBarClass('text-center');	
if($screen->submitted()) {
	$inputs = $screen->get();
	if(!$screen->errors()) {
		if(empty($inputs['directories'])) {
			// 
			Fox\Message::Error(Msg(Hive::BUNDLE,Hive::ERR_NEED_TO_SELECT_AT_LEAST_ONE_TARGET));
		} else {
			// Load All Messages
			$messageids = explode(':',$inputs['messageids']);
			foreach($messageids as $messageid) {
				if(empty($messageid)) {
					continue;
				}
				try {
					$message = \Fox\Dao::load(Message::className(),$messageid);
					foreach($inputs['directories'] as $targetData) {
						$parts = explode(':',$targetData);
						$label = $message->findLabels($messageid.':'.$parts[1].':'.$parts[0]);
						if($label->exists()) {
							continue;
						} else {
							$label->setFields(array(
								'messageid' => $messageid,
								'reference' => $parts[1],
								'reference_class_id'  => $parts[0]
							));
							$message->addToLabels($label);
						}
					}
					if($screen->buttons('moveandarchive')->pushed()) {
						$message->setFields(array('status' => Message::STATUS_ARCHIVED));
					}
					$message->prepareUpdate(Message::CHANGE_MOVETO);
					$message->update();
					if($message->mailbox()->isInSyncMode()) {
						$job = new Job();
						$directoryName = Mission::reference($parts[1]);
						$job->setFields(array('idusr' => $message->idusr,
											  'boxid' => $message->boxid,
											  'messageid' => $messageid,'type' => Job::TYPE_MOVE_TO,'params' => $directoryName));
						$job->create();
					}
				} catch(Fox\Object\Exception\NotExist $e) {
					continue;
				} catch(Fox\Object\Exception\Save $e) {
					Fox\Message::Error($e);
				}
			}
		}
	}
	echo $xview->messages(true,true);
	exit;
}
$popup = new Popup('moveToPopup',Msg(Orx\Mailer\Messages::BUNDLE,Orx\Mailer\Messages::MOVE_TO),$screen);
echo $popup->display();